package controls.demographic {
	import data.DemographicResult;
	
	import events.ExperimentEvent;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	
	public class DemographicControlClass extends VBox {
		
		[Bindable]
		public static var Q_GENDER:String = "What gender best describes you?";
		
		[Bindable]
		public static var Q_ONE:String = "What age range best describes you?";
		
		[Bindable]
		public static var Q_TWO:String = "What industry or major best describes you?";
		
		[Bindable]
		public static var GENDER_OPTIONS:ArrayCollection = new ArrayCollection(["Unspecified", "male", "female"]);
		
		[Bindable]
		public static var AGE_RANGES:ArrayCollection = new ArrayCollection(["Unspecified", "< 18", "19 - 25", "26 - 32", "33 - 39", "40 - 46", "47+"]);
		
		[Bindable]
		public static var INDUSTRY_AND_MAJORS:ArrayCollection = new ArrayCollection(["Unspecified", "Business", "Computer Science & Engineering", "Education", "Medicine", "Psychology", "Trades", "Other"]);
				
		[Bindable]
		public var emailInput:TextInput;		
		
		[Bindable]
		public var genderQuestion:DemographicQuestion;
		
		[Bindable]
		public var ageRangeQuestion:DemographicQuestion;
		
		[Bindable]
		public var industryQuestion:DemographicQuestion;
		
		[Bindable]
		public var startButton:Button;
			
		public function DemographicControlClass() {
			addEventListener(FlexEvent.CREATION_COMPLETE, handleCreationComplete);
		}
		
		private function handleCreationComplete(e:FlexEvent):void {
			genderQuestion.addEventListener(ExperimentEvent.COMBO_CHANGE, handleComboChange);
			ageRangeQuestion.addEventListener(ExperimentEvent.COMBO_CHANGE, handleComboChange);
			industryQuestion.addEventListener(ExperimentEvent.COMBO_CHANGE, handleComboChange);
		}
		
		private function handleComboChange(e:ExperimentEvent):void {
			this.startButton.enabled = determineExperimentStartEnabled();
		}
		
		public function determineExperimentStartEnabled():Boolean {
			return((this.ageRangeQuestion.optionSelected != "Unspecified") && (this.industryQuestion.optionSelected != "Unspecified") && (this.genderQuestion.optionSelected != "Unspecified"));
		}
		
		public function handleStartExperiment(event:Event):void {
			var demographicResult:DemographicResult = new DemographicResult(this.emailInput.text, this.ageRangeQuestion.answersComboBox.selectedLabel, this.industryQuestion.answersComboBox.selectedLabel);
			dispatchEvent(new ExperimentEvent(ExperimentEvent.START_EXPERIMENT, true, false, demographicResult));
		}
	}
}